#include <bits/stdc++.h>
// 2025/02/05
// tag:
// Author: Zhang Muen
using namespace std;

using int64 = int64_t;

int n, a[1000001], b[1000001], f[1000001];
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    stack<int> num, pos;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i] >> b[i];
        while (!num.empty() && num.top() < a[i]) {
            f[i] += b[pos.top()];
            num.pop();
            pos.pop();
        }
        num.push(a[i]);
        pos.push(i);
    }
    num = stack<int>(), pos = stack<int>();
    for (int i = n; i >= 1; i--) {
        while (!num.empty() && num.top() < a[i]) {
            f[i] += b[pos.top()];
            num.pop();
            pos.pop();
        }
        num.push(a[i]);
        pos.push(i);
    }
    int ans = 0;
    for (int i = 1; i <= n; i++)
        ans = max(ans, f[i]);
    cout << ans << endl;
    return 0;
}